package com.daoeping.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.daoeping.data.Bzn_answercomm;
import com.daoeping.data.Usr_user;
import com.daoeping.domain.JsonData;
import com.daoeping.service.Bzn_answercommService;
import com.daoeping.util.ConverntJsonData;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Bzn_answercommAction extends ActionSupport{
	private Bzn_answercomm bzn_answercomm;
	private Bzn_answercommService bzn_answercommService;
	private JsonData jsonData;
	
	public JsonData getJsonData() {
		return jsonData;
	}
	public void setJsonData(JsonData jsonData) {
		this.jsonData = jsonData;
	}
	public Bzn_answercommService getBzn_answercommService() {
		return bzn_answercommService;
	}
	public void setBzn_answercommService(Bzn_answercommService bzn_answercommService) {
		this.bzn_answercommService = bzn_answercommService;
	}
	public Bzn_answercomm getBzn_answercomm() {
		return bzn_answercomm;
	}
	public void setBzn_answercomm(Bzn_answercomm bzn_answercomm) {
		this.bzn_answercomm = bzn_answercomm;
	}
	public String addBzn_answercomm(){
		
		Usr_user usr_user = (Usr_user)ActionContext.getContext().getSession().get("user");
		System.out.println(usr_user.getId());
		List<Bzn_answercomm> bzn_answercommList = (List<Bzn_answercomm>)ActionContext.getContext().getSession().get("bzn_answercommList");
		if(verifyBzn_answercomm(bzn_answercommList, bzn_answercomm)){
			return "exception";
		}else{
			List<String> list = new ArrayList<String>();
			bzn_answercomm.setCreateDatetime(new Timestamp(System.currentTimeMillis()));
			bzn_answercomm.setUsr_user(usr_user);
			if(!bzn_answercommService.addBzn_answercomm(bzn_answercomm)){
				return "exception";
			};
			bzn_answercommList.add(bzn_answercomm);
			list.add("sucess_comm");
			list.add(reCaculate(bzn_answercomm)+"");
			list.add(bzn_answercomm.getBzn_answer().getId()+"");
			jsonData = new JsonData();
			jsonData.setJsonData(new ConverntJsonData().getJsonData(list).toString());
			return "addBzn_answercomm";
		}
	}
	public String findBzn_answercommByUserIdAndAnsId(){
		List<Bzn_answercomm> bzn_answercommList = (List<Bzn_answercomm>)ActionContext.getContext().getSession().get("bzn_answercommList");
		List<String> list = new ArrayList<String>();
		if(verifyBzn_answercomm(bzn_answercommList, bzn_answercomm)){
			list.add("exist_comm");
		}else{
			list.add("notexist_comm");
		}
		jsonData = new JsonData();
		jsonData.setJsonData(new ConverntJsonData().getJsonData(list).toString());
		return "findBzn_answercommByUserIdAndAnsId";
	}
	
	public boolean verifyBzn_answercomm(List<Bzn_answercomm> bzn_answercommList , Bzn_answercomm bzn_answercomm){
		boolean flag = false;
		if(bzn_answercommList!=null){
			for (Bzn_answercomm answercomm : bzn_answercommList) {
				if(answercomm.getBzn_answer().getId()==bzn_answercomm.getBzn_answer().getId()){
					flag = true;
				}
			}
		}
		return flag;
	}
	
	public double reCaculate(Bzn_answercomm bzn_answercomm){
		String string = (String)ActionContext.getContext().get(bzn_answercomm.getBzn_answer().getId()+"_score");
		double result = 0;
		if(string!=null){
			String [] strings = string.split("<");
			int number = Integer.parseInt(strings[1]);
			double average = Double.parseDouble(strings[0]);
			System.out.println(number + "==" + average);
			result = (number*average+bzn_answercomm.getRank())/(number+1);
		}else{
			result = bzn_answercomm.getRank();
		}
		
		System.out.println(result);
		return result;
	}
	
	public static void main(String[] args) {
		System.out.println(3.0*3/7);
	}
}
